package com.hdcx.Minfresh.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hdcx.Minfresh.R;
import com.hdcx.Minfresh.bean.UserInfoBean;
import com.hdcx.Minfresh.model.AccountDetailInfo;
import com.hdcx.Minfresh.url.MyUrl;
import com.hdcx.Minfresh.util.AppUtil;
import com.hdcx.Minfresh.util.ShpfUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

public class MoneyDetailActivity extends AppCompatActivity implements View.OnClickListener {

    private ListView mListViewMoney;
    private int page = 1;
    private int size = 20;
    private TextView mTextViewLoadMore;
    private List<AccountDetailInfo.ListEntity> mAccountDetaillist = new ArrayList<>();
    private MyAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_money_detail);
        findID();
        initView();
        initData();
    }

    private void initData() {
        AppUtil.showLoading(MoneyDetailActivity.this);
        JSONObject json = new JSONObject();
        UserInfoBean user = ShpfUtil.getObject(ShpfUtil.LOGIN);
        try {
            json.put("uid", user.getUserinfo().getId());
            json.put("page", page);
            json.put("size", size);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        OkHttpUtils.postString()
                .url(MyUrl.USER_ACCOUNT)
                .content(json.toString())
                .build()
                .execute(new Callback<AccountDetailInfo>() {
                    @Override
                    public AccountDetailInfo parseNetworkResponse(Response response) throws IOException {
                        String json = response.body().string();
                        Log.e("MoneyDetailActivity", "MoneyDetailActivity--parseNetworkResponse--账户记录" + json);
                        AccountDetailInfo accountDetailInfo = new Gson().fromJson(json, AccountDetailInfo.class);

                        return accountDetailInfo;
                    }

                    @Override
                    public void onError(Call call, Exception e) {
                        Log.i("onError", "onError: " + e.toString());
                    }

                    @Override
                    public void onResponse(AccountDetailInfo response) {
                        Log.e("MoneyDetailActivity", "MoneyDetailActivity--onResponse--" + response.getInfo());
                        if (response.getStatus() == 1) {
                            if (Integer.parseInt(response.getCount())> 0) {
                                if (response.getList().size() > 0) {
                                    page++;
                                    mAccountDetaillist.addAll(response.getList());
                                    mAdapter.notifyDataSetChanged();
                                } else {
                                    mTextViewLoadMore.setText("没有更多记录了");
                                }
                            } else {
                                mTextViewLoadMore.setText("没有记录");
                            }
                        } else {

                        }
                        AppUtil.closeLoading();
                    }
                });
    }

    private void findID() {
        mListViewMoney = (ListView) findViewById(R.id.listViewMoney);
        mTextViewLoadMore = (TextView) findViewById(R.id.textViewLoadMore);
        mTextViewLoadMore.setOnClickListener(this);
    }

    private void initView() {
        mAdapter = new MyAdapter();
        mListViewMoney.setAdapter(mAdapter);
        mTextViewLoadMore.setText("加载更多");
        TextView title = (TextView) findViewById(R.id.tv_title);
        title.setText("账户记录");
        findViewById(R.id.tv_left).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.textViewLoadMore:
                initData();
                break;
            case R.id.tv_left:
                finish();
                break;
        }
    }

    class MyAdapter extends BaseAdapter {
        class ViewHolder {
            public TextView textViewInfo;
            public TextView textViewTime;
            public TextView textViewMoney;
        }

        @Override
        public int getCount() {
            return mAccountDetaillist.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = getLayoutInflater().inflate(R.layout.money_detail_item, null);
                holder.textViewInfo = (TextView) convertView.findViewById(R.id.textViewInfo);
                holder.textViewTime = (TextView) convertView.findViewById(R.id.textViewTime);
                holder.textViewMoney = (TextView) convertView.findViewById(R.id.textViewMoney);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            AccountDetailInfo.ListEntity listEntity = mAccountDetaillist.get(position);
            holder.textViewInfo.setText(listEntity.getTitle());
            holder.textViewTime.setText(listEntity.getAdd_time());
            holder.textViewMoney.setText(listEntity.getMoney() + "");
            return convertView;
        }
    }
}
